//
//  SaveFile.m
//  DouBanDemo
//
//  Created by LiuFei on 14/11/7.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "SaveFile.h"

@implementation SaveFile

// 根据名字获取路径
+ (NSString *)pathWithName:(NSString *)name
{
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [arr lastObject];
    path = [NSString stringWithFormat:@"%@/%@", path, name];
    return path;
}


// 判断文件是否存在
+ (BOOL)judgeFileWithName:(NSString *)name
{
    NSString *path = [SaveFile pathWithName:name];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return YES;
    } else {
        return NO;
    }
}

// 读取文件
+ (NSData *)readFileWithName:(NSString *)name
{
    if ([SaveFile judgeFileWithName:name]) {
        NSString *path = [SaveFile pathWithName:name];
        NSData *data = [NSData dataWithContentsOfFile:path];
        return data;
    }
    return nil;
}

// 存储文件
+ (BOOL)writeFileWithName:(NSString *)name data:(NSData *)data
{
    NSString *path = [SaveFile pathWithName:name];
    NSLog(@"path = %@", path);
    return [data writeToFile:path atomically:YES];
}









@end
